package cn.chenxing.module.query.controller;

import cn.chenxing.domain.dto.TrackTable;
import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.utils.CommonUtil;
import cn.chenxing.utils.TableUtil;
import com.jfoenix.controls.JFXComboBox;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description 用户查询的控制层
 * @Author maogen.ymg
 * @Date 2020/4/14 23:18
 */
@Lazy
@FXMLController
@Slf4j
public class QueryUserController implements Initializable {

    @FXML
    private JFXComboBox<String> userList;

    @FXML
    private BorderPane borderPane;

    @FXML
    private Label tipLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        CommonUtil.updateList(userList, false);
    }

    @FXML
    private void updateUser() {
        CommonUtil.updateList(userList, false);
        DialogBuilderUtil.showPositiveAlert(userList, "提示", "更新列表成功");
    }

    @FXML
    public void query() {
        DialogBuilderUtil.showPositiveAlert(userList.getValue() == null, userList, "提示", "数据还未导入，请先导入文件数据");

        // 显示在表格上
        TableUtil.importTableView(borderPane, getTableData(userList.getValue()), false);

    }

    @FXML
    public void clear() {
        borderPane.getChildren().clear();
        borderPane.setCenter(tipLabel);
    }

    /**
     * 获取表格数据
     * @return List<TrackTable>
     */
    private List<TrackTable> getTableData(String userStr) {

        return CommonUtil.getTableDataByUserId(userStr, true);
    }

}
